package welsir.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import welsir.entity.dos.ArticleClass;
import welsir.entity.dos.Square;
import welsir.entity.dos.SquareClass;
import welsir.entity.vo.ResultMessage;
import welsir.entity.vo.SquareClassPageVO;
import welsir.mapper.ArticleClassMapper;
import welsir.mapper.SquareClassMapper;
import welsir.mapper.SquareMapper;
import welsir.service.IArticleClassService;
import welsir.service.ISquareClassService;
import welsir.util.ResultUtil;

import javax.annotation.Resource;
import javax.xml.transform.Result;
import java.util.List;

/**
 * @author Genius
 * @date 2023/03/08 17:59
 * @admin
 **/
@io.swagger.annotations.Api(tags = "后台圈子分类接口")
@RestController
@RequestMapping("/squareClass")
public class SquareClassController {

    @Autowired
    private IArticleClassService articleClassService;

    @Resource
    private ArticleClassMapper articleClassMapper;

    @Autowired
    private ISquareClassService squareClassService;

    @Resource
    private SquareClassMapper squareClassMapper;

    @Resource
    private SquareMapper squareMapper;

    @ApiOperation(value = "新建分类")
    @ApiImplicitParam(name = "newSquareClass",value = "圈子分类对象",required = true)
    @PostMapping("/newSquareClass")
    public ResultMessage<Integer> newSquareClass(@RequestBody SquareClass squareClass) {
        QueryWrapper<SquareClass> wrapper= new QueryWrapper<SquareClass>();
        wrapper.eq("name", squareClass.getName());
        SquareClass squareClassJudge = squareClassService.getOne(wrapper);
        if(squareClassJudge != null){
            //该分类已存在
            return ResultUtil.error(143,"该分类已存在");
        }
        this.squareClassMapper.insert(squareClass);
        return ResultUtil.success();
    }


    @ApiOperation(value = "修改圈子分类")
    @ApiImplicitParam(name = "updateSquareClass",value = "圈子分类对象修改",required = true)
    @PostMapping("/updateSquareClass")
    public ResultMessage<Integer> updateSquareClass(@RequestBody SquareClass squareClass) {
        QueryWrapper<SquareClass> wrapper= new QueryWrapper<SquareClass>();
        wrapper.eq("id", squareClass.getId());
        SquareClass squareClassJudge = squareClassService.getOne(wrapper);
        if(squareClassJudge != null){
            //该分类已存在 可以修改
            squareClassMapper.update(squareClass,wrapper);
            return ResultUtil.success();
        }
        else{
            return ResultUtil.error(143,"该分类不存在");
        }
    }

    @ApiOperation(value = "获取圈子分类列表(分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数",required = true),
            @ApiImplicitParam(name = "limit",value = "总量",required = true)
    })
    @PostMapping("/allSquareClass/{page}/{limit}")
    public ResultMessage<SquareClassPageVO> allSquareClass(
            @PathVariable("page") Integer page,
            @PathVariable("limit") Integer limit
    ) {
        return ResultUtil.data(squareClassService.GetList(page,limit));
    }

    @ApiOperation(value = "获取全部分类列表")
    @GetMapping("/getAllClassName")
    public ResultMessage<List<SquareClass>> getAllClassName(){

        QueryWrapper<SquareClass> wrapper= new QueryWrapper<SquareClass>();
        wrapper.orderByDesc("id");

        return  ResultUtil.data(squareClassMapper.selectList(wrapper));
    }

    @ApiOperation(value = "根据id删除圈子分类评论")
    @ApiImplicitParam(name = "id",value = "文章id",required = true)
    @GetMapping("/deleteSquareClassById/{id}")
    public ResultMessage<Boolean> deleteSquareClassById(
            @PathVariable("id") Integer id
    ) {
        //根据id删除
        QueryWrapper<Square> wrapper= new QueryWrapper<Square>();
        wrapper.eq("sort_class",id);
        squareMapper.delete(wrapper);
        return ResultUtil.data(this.squareClassService.removeById(id));
    }

    @ApiOperation(value = "根据id值查询对应的分类名称")
    @ApiImplicitParam(name = "id",value = "id",required = true)
    @GetMapping("/getClassNameById/{id}")
    public ResultMessage<String> getClassNameById(
            @PathVariable("id") Integer id
    ){
        QueryWrapper<SquareClass> wrapper= new QueryWrapper<SquareClass>();
        wrapper.eq("id",id);

        SquareClass squareClass = squareClassMapper.selectOne(wrapper);
        return ResultUtil.data(squareClass.getName());
    }
}
